
package com.netease.xone.widget.msgbubble;

import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.callbacks.QueryCallback;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.Manifold;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.contacts.Contact;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityAdviceList;
import com.netease.xone.activity.ActivityDynamicList;
import com.netease.xone.widget.msgbubble.MessageBubble.OnBubbleClickListener;

/**
 * 消息泡渲染逻辑
 *
 * @author MR
 */
public class MessageBubbleRender implements Renderable, OnTouchListener, OnBubbleClickListener {

    public static final String TAG = "MessageBubbleRender";

    private MyBox2d mBox2d;
    private MessageBubble mDynamicBubble;// 动态
    private MessageBubble mAdviceBubble;// 通知
    private MessageBubble mPmBubble;// 私信
    private MessageBubble mPraiseBubble;// 赞
    private MessageBubble mAtBubble;// @我
    private MessageBubble mCommentBubble;// 评论
    private Body mWall;
    private int mWidth, mHeight;
    private Context mContext;

    public MessageBubbleRender(Context context) {
        mContext = context;
        // int screenWidth =
        // context.getResources().getDisplayMetrics().widthPixels;
        // int screenHeight =
        // context.getResources().getDisplayMetrics().heightPixels;
    }

    private void init(int width, int height) {
        // 计算参数
        int minLength = width < height ? width : height;// 以最短边为基准
        int radius = minLength / 4 / 2;// 以最短边容纳4个气泡为基准
        // 构建Box2d
        mBox2d = new MyBox2d();
        mWall = mBox2d.createWall(0, 0, width / MyBox2d.SCALE_RATE, height / MyBox2d.SCALE_RATE);
        mBox2d.getWorld().setContactListener(mContactListener);

        //分配坐标
        int[] x = new int[3];
        int[] y = new int[2];
        int areaWidth = width / 3;
        int areaHeight = height / 2;
        x[0] = areaWidth / 2;
        x[1] = areaWidth + areaWidth / 2;
        x[2] = areaWidth * 2 + areaWidth / 2;
        y[0] = areaHeight / 2;
        y[1] = areaHeight + areaHeight / 2;

        {
            // 1好友动态
            Drawable icon = mContext.getResources().getDrawable(R.drawable.icon_msg_friend);
            Body bubbleBody = mBox2d.createCircle(x[0] / MyBox2d.SCALE_RATE, y[0]
                    / MyBox2d.SCALE_RATE, radius / MyBox2d.SCALE_RATE, false);
            bubbleBody.setLinearVelocity(new Vec2(-4f, -9f));
            mDynamicBubble = new MessageBubble(icon, null, bubbleBody,
                    mContext.getString(R.string.msgbubble_dynamic), true);
            mDynamicBubble.setOnBubbleClickListener(this);
        }
        {
            // 2通知
            Drawable icon = mContext.getResources().getDrawable(R.drawable.icon_msg_sys);
            Body bubbleBody = mBox2d.createCircle(x[1] / MyBox2d.SCALE_RATE, y[0]
                    / MyBox2d.SCALE_RATE, radius / MyBox2d.SCALE_RATE, false);
            bubbleBody.setLinearVelocity(new Vec2(-2f, 5f));
            mAdviceBubble = new MessageBubble(icon, null, bubbleBody,
                    mContext.getString(R.string.msgbubble_advice), true);
            mAdviceBubble.setOnBubbleClickListener(this);
        }
        {
            // 3私信
            Drawable icon = mContext.getResources().getDrawable(R.drawable.icon_msg_pm);
            Body bubbleBody = mBox2d.createCircle(x[2] / MyBox2d.SCALE_RATE, y[0]
                    / MyBox2d.SCALE_RATE, radius / MyBox2d.SCALE_RATE, false);
            bubbleBody.setLinearVelocity(new Vec2(4f, -3f));
            mPmBubble = new MessageBubble(icon, null, bubbleBody,
                    mContext.getString(R.string.msgbubble_private_message), true);
            mPmBubble.setOnBubbleClickListener(this);
        }
        {
            // 4赞我
            Drawable icon = mContext.getResources().getDrawable(R.drawable.icon_msg_fav);
            Body bubbleBody = mBox2d.createCircle(x[0] / MyBox2d.SCALE_RATE, y[1]
                    / MyBox2d.SCALE_RATE, radius / MyBox2d.SCALE_RATE, false);
            bubbleBody.setLinearVelocity(new Vec2(6f, 4f));
            mPraiseBubble = new MessageBubble(icon, null, bubbleBody,
                    mContext.getString(R.string.msgbubble_prise), true);
            mPraiseBubble.setOnBubbleClickListener(this);
        }
        {
            // 5@我
            Drawable icon = mContext.getResources().getDrawable(R.drawable.icon_msg_at);
            Body bubbleBody = mBox2d.createCircle(x[1] / MyBox2d.SCALE_RATE, y[1]
                    / MyBox2d.SCALE_RATE, radius / MyBox2d.SCALE_RATE, false);
            bubbleBody.setLinearVelocity(new Vec2(2f, -5f));
            mAtBubble = new MessageBubble(icon, null, bubbleBody,
                    mContext.getString(R.string.msgbubble_at), true);
            mAtBubble.setOnBubbleClickListener(this);
        }
        {
            // 6评论我
            Drawable icon = mContext.getResources().getDrawable(R.drawable.icon_msg_comment);
            Body bubbleBody = mBox2d.createCircle(x[2] / MyBox2d.SCALE_RATE, y[1]
                    / MyBox2d.SCALE_RATE, radius / MyBox2d.SCALE_RATE, false);
            bubbleBody.setLinearVelocity(new Vec2(2f, -9f));
            mCommentBubble = new MessageBubble(icon, null, bubbleBody,
                    mContext.getString(R.string.msgbubble_comment), true);
            mCommentBubble.setOnBubbleClickListener(this);
        }
        {
            // Test
            mPraiseBubble.setMessageCount(1);
            mAdviceBubble.setMessageCount(2);
            mAtBubble.setMessageCount(5);
        }
    }

    @Override
    public int getFrameHz() {
        return 60;
    }

    @Override
    public void doDraw(Canvas c) {
        c.save();
        c.drawColor(0xff6b7578);
        mDynamicBubble.draw(c);// 动态
        mAdviceBubble.draw(c);// 通知
        mPmBubble.draw(c);// 私信
        mPraiseBubble.draw(c);// 赞
        mAtBubble.draw(c);// @我
        mCommentBubble.draw(c);// 评论
        c.restore();
    }

    @Override
    public void updateState() {
        mBox2d.logic(1f / getFrameHz());
    }

    @Override
    public void onSurfaceCreated(int width, int height) {
        mWidth = width;
        mHeight = height;
        init(width, height);
        if (mWall == null) {

        }
    }

    @Override
    public void onSurfaceChanged(int width, int height) {
        mWidth = width;
        mHeight = height;
        if (mWall == null) {
            mWall = mBox2d
                    .createWall(0, 0, width / MyBox2d.SCALE_RATE, height / MyBox2d.SCALE_RATE);
        }
    }

    private ContactListener mContactListener = new ContactListener() {

        @Override
        public void preSolve(Contact arg0, Manifold arg1) {

        }

        @Override
        public void postSolve(Contact arg0, ContactImpulse arg1) {

        }

        @Override
        public void endContact(Contact arg0) {

        }

        @Override
        public void beginContact(Contact contact) {
            if (contact != null) {
                Object obj = contact.getFixtureA().getBody().getUserData();
                Object obj2 = contact.getFixtureB().getBody().getUserData();
                if (obj != null && obj instanceof MessageBubble) {
                    MessageBubble b = (MessageBubble)obj;
                    b.startJiggle();
                }
                if (obj2 != null && obj2 instanceof MessageBubble) {
                    MessageBubble b = (MessageBubble)obj2;
                    b.startJiggle();
                }
            }
        }
    };

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                onTouchBubble(event.getX() / MyBox2d.SCALE_RATE, event.getY() / MyBox2d.SCALE_RATE);
                break;
        }
        return false;
    }

    private void onTouchBubble(final float touchX, final float touchY) {
        AABB queryAABB = new AABB();
        queryAABB.lowerBound.set(touchX - .001f, touchY - .001f);
        queryAABB.upperBound.set(touchX + .001f, touchY + .001f);

        QueryCallback callBack = new QueryCallback() {

            @Override
            public boolean reportFixture(Fixture fixture) {
                if (fixture.getBody() != null
                        && fixture.getBody().getUserData() instanceof MessageBubble) {
                    MessageBubble b = (MessageBubble)fixture.getBody().getUserData();
                    b.onTouch(touchX, touchY);
                }
                return false;
            }
        };
        mBox2d.getWorld().queryAABB(callBack, queryAABB);
    }

    @Override
    public void onBubbleClick(String title) {
        if (title.equals(mContext.getString(R.string.msgbubble_dynamic))) {
            NTLog.d(TAG, "好友动态 bubble click");
            ActivityDynamicList.startActivity(mContext);
        } else if (title.equals(mContext.getString(R.string.msgbubble_advice))) {
            NTLog.d(TAG, "通知 bubble click");
            ActivityAdviceList.startActivity(mContext);
        }
    }

}
